package com.sgai.dcac.data.model;

import com.sgai.dcac.AppApplication;
import com.sgai.dcac.contract.LoginContract;
import com.sgai.dcac.data.entity.UserInfo;
import com.sgai.dcac.data.remote.LoginService;
import com.sgai.dcac.utils.UUIDUtil;
import com.sgai.dcac.utils.UserUtil;

import retrofit2.Retrofit;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func0;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by tony on 2017/6/14.
 */

public class LoginModel implements LoginContract.Model {

    private Retrofit retrofit;
    private LoginService loginService;

    @Override
    public void doLogin(String username, String password, final LoginContract.Presenter presenter) {
        retrofit = AppApplication.getInstance().getNormalRetrofit();
        loginService = retrofit.create(LoginService.class);
        Observable<UserInfo> observable = loginService.doLogin(username,password, UUIDUtil.getMyUUID(AppApplication.getInstance()));
//        observable.subscribeOn(Schedulers.io())
//                .observeOn(Schedulers.newThread())
//                .subscribe(new Observer<UserInfo>() {
//                    @Override
//                    public void onCompleted() {
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onNext(UserInfo userInfo) {
//
//                    }
//                });

        observable
                .observeOn(Schedulers.io())
                .subscribeOn(Schedulers.io())
                .flatMap(new Func1<UserInfo, Observable<UserInfo>>() {
                    @Override
                    public Observable<UserInfo> call(UserInfo userInfo) {
                        //做存储处理
                        UserUtil.setCurrentUserInfo(userInfo);
                        return Observable.just(userInfo);
                    }
                }, new Func1<Throwable, Observable<UserInfo>>() {
                    @Override
                    public Observable<UserInfo> call(final Throwable throwable) {
                        return Observable.create(new Observable.OnSubscribe<UserInfo>() {
                            @Override
                            public void call(Subscriber<? super UserInfo> subscriber) {
                                subscriber.onError(throwable);
                            }
                        });
                    }
                }, new Func0<Observable<UserInfo>>() {
                    @Override
                    public Observable<UserInfo> call() {
                        return null;
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        //需要分析 网络错误/存储
                        presenter.failure(e.toString());
                    }

                    @Override
                    public void onNext(UserInfo userInfo) {
                        presenter.success("登录成功!");
                    }
                });

    }
}
